<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="showTitle" :visible="showDialog" @close="handleClose">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="formRef" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in people" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button type="primary" size="small" @click="submit">确定</el-button>
      <el-button size="small" @click="handleClose">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { reqGetEmployeesSimple } from '@/api/employees'
import { reqAddDepartment, reqGetDepartDetail, reqUpdateDepartments } from '@/api/departments'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    deptsList: {
      type: Array,
      required: true
    },
    nodeData: {
      type: Object,
      required: true
    }
  },
  data() {
    const checkNameRepeat = (rule, value, callback) => {
      let arr = []
      if (this.form.id) {
        if (this.nodeData.name === value) {
          return callback()
        }
        arr = this.deptsList.filter(item => item.pid === this.nodeData.pid)
      } else {
        arr = this.deptsList.filter(item => item.pid === this.nodeData.id)
      }
      const flag = arr.some(item => item.name === value)
      flag ? callback(new Error(`同级部门下已经有${value}的部门了`)) : callback()
    }
    const checkCodeRepeat = (rule, value, callback) => {
      if (this.form.id) {
        return callback()
      } else {
        const flag = this.deptsList.some(item => item.code === value)
        flag ? callback(new Error(`已经有编码${value}的部门了`)) : callback()
      }
    }
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: ['blur', 'change'] },
          { validator: checkNameRepeat, trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: ['blur', 'change'] },
          { validator: checkCodeRepeat, trigger: ['blur', 'change'] }
        ],
        manager: [
          { required: true, message: '请输入部门负责人', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '长度在 1 到 300 个字符', trigger: ['blur', 'change'] }
        ]
      },
      people: []
    }
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getEmployeesSimple()
  },
  methods: {
    handleClose() {
      this.$emit('update:showDialog', false)
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$refs.formRef.resetFields()
    },
    async getEmployeesSimple() {
      const { data } = await reqGetEmployeesSimple()
      // console.log('简单员工列表', data)
      this.people = data
    },
    async submit() {
      try {
        // 预校验
        await this.$refs.formRef.validate()
        if (this.form.id) {
          await reqUpdateDepartments(this.form)
        } else {
          await reqAddDepartment({ ...this.form, pid: this.nodeData.id })
        }
        // 成功提示
        this.$message.success('新增部门成功')
        // 更新渲染
        this.$emit('add-dept')
        // 关闭弹层
        this.handleClose()
      } catch (error) {
        console.log('校验失败')
      }
    },
    async getDepartDetail(id) {
      const { data } = await reqGetDepartDetail(id)
      console.log(data)
      this.form = data
    }
  }
}
</script>

<style>

</style>
